package com.lottemall.item.web;

import com.lottemall.item.pojo.SpecGroup;
import com.lottemall.item.pojo.SpecParam;
import com.lottemall.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 统一的处理规格参数的控制层
 * <p>
 * 前台根据商品类型id查询规格组的url    http://api.lottemall.com/api/item/spec/groups/1023
 * 前台根据规格组id查询规格参数的url    http://api.lottemall.com/api/item/spec/params?gid=1
 * 前台根据规商品分类id查询规格参数的url    http://api.lottemall.com/api/item/spec/params?cid=77
 */
@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 根据商品类id查询对应的所有规格组信息
     */
    @RequestMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid) {
        return ResponseEntity.ok(specificationService.queryGroupByCid(cid));
    }


    /**
     * 查询规格参数的集合
     * 是动态的
     * @param gid 规格组gid
     * @param cid 商品类型cid
     * @param searching 是否搜索
     * @return
     */
    @RequestMapping("params")
    public ResponseEntity<List<SpecParam>> queryParamByList(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specificationService.queryParamList(gid,cid,searching));
    }


    /**
     * 根据商品分类id查询规格组以及组内参数
     * @param cid
     * @return
     */
    @GetMapping("group")
    public ResponseEntity<List<SpecGroup>> queryListByCid(@RequestParam("cid") Long cid){
        return ResponseEntity.ok(specificationService.queryListByCid(cid));

    }

}
